package com.wang.gulimall.product.app;

import com.wang.common.utils.PageUtils;
import com.wang.common.utils.R;
import com.wang.gulimall.product.entity.AttrEntity;
import com.wang.gulimall.product.entity.AttrGroupEntity;
import com.wang.gulimall.product.service.AttrAttrgroupRelationService;
import com.wang.gulimall.product.service.AttrGroupService;
import com.wang.gulimall.product.service.AttrService;
import com.wang.gulimall.product.service.CategoryService;
import com.wang.gulimall.product.vo.AttrGroupRelationVo;
import com.wang.gulimall.product.vo.AttrGroupWithAttrsVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 属性分组
 *
 * @author wangliuyang
 * @email 1842184157@qq.com
 * @date 2021-01-12 16:27:31
 */
@RestController
@RequestMapping("product/attrgroup")
@Slf4j
public class AttrGroupController {

    @Autowired
    private AttrGroupService attrGroupService;

    @Autowired
    CategoryService categoryService;

    @Autowired
    AttrService attrService;

    @Autowired
    AttrAttrgroupRelationService relationService;

    /**
     * Desc:查询属性分组的关联
     *
     * @param attrgroupId
     * @return {@link R}
     * @author wangliuyang
     * @date
     */
    @GetMapping("/{attrgroupId}/attr/relation")  //product/attrgroup/{attrgroupId}/attr/relation
    public R attrRelation(@PathVariable("attrgroupId") Long attrgroupId) {
        List<AttrEntity> entities = attrService.getRelationAttr(attrgroupId);
        return R.ok().put("data", entities);
    }


    //获取属性分组的所有非关联属性 //product/attrgroup/{attrgroupId}/noattr/relation
    @GetMapping("/{attrgroupId}/noattr/relation")
    public R attrNoRelation(@PathVariable("attrgroupId") Long attrgroupId,
                            @RequestParam Map<String, Object> params) {
        PageUtils page = attrService.getNoRelationAttr(params, attrgroupId);
        return R.ok().put("page", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);
        return R.ok().put("page", page);
    }

    /**
     * 列表  catelogId路径变量
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable("catelogId") Long catelogId) {
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        // 用当前当前分类id查询完整路径并写入 attrGroup
        Long[] path = categoryService.findCateLogPath(attrGroup.getCatelogId());
        attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * @param vos:
     * @Description: 新建关联确认新增关联 新增属性分组与属性的关联关系
     * @Author: YH
     * @Date: 2021/5/16
     * @return: com.wang.common.utils.R
     **/
    @PostMapping("/attr/relation")     ///product/attrgroup/attr/relation
    public R addRelation(@RequestBody List<AttrGroupRelationVo> vos) {
        //vos[AttrGroupRelationVo(attrId=5, attrGroupId=2)]
        //属性id为5关联2号分组
        relationService.saveBatch(vos);
        return R.ok();
    }


    //当前属性下的所有属性分组 下一步设置基本参数
    @GetMapping("/{catelogId}/withattr")   //product/attrgroup/{catelogId}225/
    public R getAttrGroupWithAttrs(@PathVariable("catelogId") Long catelogId) {

        //1、查出当前分类下的所有属性分组，
        //2、查出每个属性分组的所有属性
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatelogId(catelogId);
        return R.ok().put("data", vos);
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 属性分组删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

    /**
     * @param vos:
     * @Description:属性分组删除
     * @Author: YH
     * @Date: 2021/5/15
     * @return: com.wang.common.utils.R
     **/
    @PostMapping("/attr/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos) {
        //前端传回参数 attrID  attrgroupId
        attrService.deleteRelation(vos);
        return R.ok();
    }
}
